import { createSlice } from '@reduxjs/toolkit';
const userSlice = createSlice({
    name: 'user',
    initialState: {
        name: '张三',
        count: 0,
        user: [{
            id: 1,
            name: '张三'
        }, {
            id: 2,
            name: '李四'
        }]
    },
    reducers: {
        setName(state, action) {
            state.user.forEach(it => {
                if (it.id == action.payload.id) {
                    it.name = action.payload.name
                }
            })
        },
        addcount(state, action) {
            state.count += action.payload
        }
    }
})
export const countAdd = (n) => (dispatch) => {
    setTimeout(() => {
        dispatch(userSlice.actions.addcount(n))
    }, 2000)
}
export const nameSet = (n) => (dispatch) => {
    setTimeout(() => {
        dispatch(userSlice.actions.setName(n))
    }, 2000)
}
export default userSlice.reducer
export const { setName } = userSlice.actions